/****************************************************************
*
*	bacth, March 27, 2008
*
****************************************************************/

//****************************************************************************************
//***** library **************************************************************************
var ie = window.navigator.appName == 'Microsoft Internet Explorer';

// get lowest parent node of child node
function getParentFromChild(childNode, parentTag)
{
	var i = 0;
	while (childNode.nodeName.toLowerCase() != parentTag.toLowerCase() && (i++)<10) childNode = childNode.parentNode;
	return childNode;
}

// set selected value of dropdownlist
function selectDropDownList(cbo, selectedValue)
{
	for (var i=0; i<cbo.options.length; i++)
	{
		if (cbo.options[i].value == selectedValue)
		{
			cbo.options[i].selected = true;
			return;
		}
	}
}

// set attribute for a node, cross-browser
function setAttribute(element, attributeName, attributeValue)
{
	// firefox
	element.setAttribute(attributeName, attributeValue);
	// ie
	for (var i=0;i<element.attributes.length; i++)
	{
		if (element.attributes[i].name.toLowerCase()==attributeName.toLowerCase() && attributeName != 'style')
		{
			element.attributes[i].value = attributeValue;
			break;
		}
	}
	
}

// convert number input => width style
function parseWidth(width)
{
	if (width == null || width == '' || width == 'auto' || width == '?') return 'auto';
	
	if (width.indexOf('%')==-1 && width.indexOf('px')==-1)
		return width + 'px';
	else
	{
		return width;
	}
}

// validate width for style
// right format: 56%, 56px, 56, auto
function validateWidth(width)
{
	if (width == null || width == '' || width == 'auto') return true;
	
	width = width.replace('%', '');
	width = width.replace('px', '');
	
	return !isNaN(width);
}

// trap enter key
// ev: event
// el: element fired event
function doBlur(ev, el)
{
	var key;

	if(window.event)
		key = window.event.keyCode;     //IE
	else
		key = ev.which;     //firefox

	if (key == 13)
	{
		el.blur();
		return false;
	}
}

//
function setFloat(element, floatValue)
{
	if (ie)
		element.style.styleFloat = floatValue; 
	else
		element.style.cssFloat = floatValue;
}


// 
function getChidlNode(parentNode, childTag, index)
{
	var childs = parentNode.childNodes;
	var count = 0;
	for (var i=0; i<childs.length; i++)
	{
		if (childs[i].nodeName.toLowerCase() == childTag)
		{
			if (count == index) return childs[i];
			count++;
		}
	}
}

//
function  getChildsOfParent(parentNode, childTag)
{
	var toReturn = new Array();
	var childs = parentNode.childNodes;
	for (var i=0; i<childs.length; i++)
	{
		if (childs[i].nodeName.toLowerCase() == childTag)
		{
			toReturn.push(childs[i]);
		}
	}
	return toReturn;
}
function getOffsetLeft(el)
{
	var offset = 0;
	while (el != document.documentElement)
	{
		offset += el.offsetLeft;
		el = el.parentNode;
	}
	return offset;
}
function getOffsetTop(el)
{
	var offset = 0;
	while (el != document.documentElement)
	{
		offset += el.offsetTop;
		if (el.nodeName.toLowerCase() == 'td')
			el = el.parentNode;
		el = el.parentNode;
	}
	return offset;
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
function findPosX(obj)
	{
		var curleft = 0;
		if(obj.offsetParent)
			while(1) 
			{
				curleft += obj.offsetLeft;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		else if(obj.x)
			curleft += obj.x;
		return curleft;
	}

function findPosY(obj)
	{
		var curtop = 0;
		if(obj.offsetParent)
		while(1)
		{
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
			break;
			obj = obj.offsetParent;
		}
		else if(obj.y)
			curtop += obj.y;
		return curtop;
	}
	

function trimstr(str) {return str.replace(/^\s+|\s+$/g, '');}	

function openpreview(sUrl,w,h)
{	
	var winX = 0;
    var winY = 0;
    if (parseInt(navigator.appVersion) >= 4)
    {
	    winX = (screen.availWidth - w)*.5;
	    winY = (screen.availHeight - h)*.5;
    }
    var newWindow = window.open(sUrl, '', 'scrollbars,resizable=yes,status=yes, width=' + w + ',height=' + h + ',left=' + winX + ',top=' + winY);
}
function getNodeByClass(parentNode, xpath)
{
	var tagName = xpath.split('.')[0];
	var className = xpath.split('.')[1];
	
	var node, nodes = parentNode.getElementsByTagName(tagName);
	for (var i=0; i<nodes.length; i++)
	{
		node = nodes[i];
		if (node.className.toLowerCase() == className.toLowerCase()) return node;
	}
	return null;
}

function setValueDropdownlist(cbo, value)
	{
	
		if (cbo && value)
		{
			value = trimstr(value);
			value = value.split(' ')[0];
			for (var i=0; i<cbo.options.length; i++)
			{
				if (cbo.options[i].value == value) cbo.selectedIndex = i;
			}
		}
	}

function hideAllDropDown()
{
	var i = 0;
    var elm = document.getElementsByTagName('select')
    for(i=0;i<elm.length;i++)
    {                
        if(elm[i].type.toLowerCase() == 'select-one')
        {                    
            elm[i].style.visibility = 'hidden';
        }
    }
}
function showAllDropDown()
{
	var i = 0;
    var elm=document.forms[0].elements;
    for(i=0;i<elm.length;i++)
    {                
        if(elm[i].type && elm[i].type.toLowerCase() == 'select-one')
        {                    
            elm[i].style.visibility = 'visible';
        }
    }
}